package gov.nhso.prov.acc.ftp;

import gov.nhso.prov.acc.core.ConfigBundle;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class FTPUtil {
	private static final Locale LOCALE_EN = new Locale("EN","en");
	public String schema = ConfigBundle.getValue("SCHEMA");
	public String ftpHost = ConfigBundle.getValue("FTP_HOST");
	public String ftpUserId = ConfigBundle.getValue("FTP_USERID");
	public String ftpPasswd = ConfigBundle.getValue("FTP_PASSWD");
	public String ftpNhso2BaacTranfer = ConfigBundle.getValue("FTP_NHSO2BAAC_TRANFER");
	public String ftpBaac2NhsoTranfer = ConfigBundle.getValue("FTP_BAAC2NHSO_TRANFER");
	public String ftpBaac2NhsoCheque = ConfigBundle.getValue("FTP_BAAC2NHSO_CHEQUE");
	public String branchCode = ConfigBundle.getValue("BRANCH_CODE");
	//public String ftpBackup = ConfigBundle.getValue("FTP_BACKUP");
	
	public String numberFormat(String format, String number) {
		String result = "";
        try {
        	if (number != null && !number.equals("")) {
	        	NumberFormat formatter = new DecimalFormat(format);
	        	result = formatter.format(Double.parseDouble(number.replaceAll("[^.0-9\\.]+", "")));
        	}
        } catch (Exception e) {
        	e.printStackTrace();
		}
        return result;
	}
	public String formatDecimal(double d, int precision){
		String myformat = "###,###,###,###,##0";
		String result = "";
		if (precision == 0){
			DecimalFormat df = new DecimalFormat(myformat);
			return df.format(d).replaceAll("[^.0-9\\.]+", "");
		}
		myformat = "###############.";

		for(int x= 0; x < precision; x++)
		myformat = myformat + "0";

		DecimalFormat df = new DecimalFormat(myformat);
		result = df.format(d);
		result = result.replace(",", "");
		result = result.replace(".", "");
		if (result.length() < 15) {
			int length = 15 - result.length();
			for (int i=0;i<length;i++) {
				result = "0"+result;
			}
		}
		return result;
	}
	public String fulFill(String str, int maxLength){
		String result = "";
		if (str != null && str.length() <= maxLength) {
			StringBuffer bufferStr = new StringBuffer();
			int i=0;
			bufferStr.append(str);
			if(str.length() < maxLength){
				for(i = str.length();i<maxLength;i++){
					bufferStr.append(" ");
				}
			}
			result = bufferStr.toString();
		} else if (str != null && str.length() > maxLength) {
			result = str.substring(0, maxLength);
		} else {
			StringBuffer bufferStr = new StringBuffer();
			for(int i = 0;i<maxLength;i++){
				bufferStr.append(" ");
			}
			result = bufferStr.toString();
		}
		return result;
	}
	
	public String getCurrentDate(){
        SimpleDateFormat ssdd = new SimpleDateFormat("ddMMyyyy", LOCALE_EN);
        return ssdd.format(new Date());
    }
	
	public String yy(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yy", LOCALE_EN);
        return ssdd.format(new Date());
    }
	
	public String yyyy(){
        SimpleDateFormat ssdd = new SimpleDateFormat("yyyy", LOCALE_EN);
        return ssdd.format(new Date());
    }

    public String mm(){
        SimpleDateFormat ssdd = new SimpleDateFormat("MM", LOCALE_EN);
        return ssdd.format(new Date());
    }
    
    public String dd(){
        SimpleDateFormat ssdd = new SimpleDateFormat("dd", LOCALE_EN);
        return ssdd.format(new Date());
    }
    public String getTomorrow(){
		Calendar cal = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("dd", LOCALE_EN);
		cal.add(Calendar.DATE, +1);
        return dateFormat.format(cal.getTime());
    }
    public String getTomorrow(int day){
		Calendar cal = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", LOCALE_EN);
		cal.add(Calendar.DATE, +day);
        return dateFormat.format(cal.getTime());
    }
    public String getNextDayFullFormat(int day){
		Calendar cal = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", LOCALE_EN);
		cal.add(Calendar.DATE, +day);
        return dateFormat.format(cal.getTime());
    }
    
    public String getNextDayFullFormat(int yyyy, int mm , int dd){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
	    cal.set(yyyy, mm, dd);
        return dateFormat.format(cal.getTime());
    }

    public String getTomorrow(int yyyy, int mm , int dd){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	    cal.set(yyyy, mm, dd);
        return dateFormat.format(cal.getTime());
    }
    public String yesterDay2(){
		Calendar cal = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", LOCALE_EN);
		cal.add(Calendar.DATE, -1);
        return dateFormat.format(cal.getTime());
    }
    
    public String setTimeStamp2DB(String ts){
    	return "TO_TIMESTAMP('" + ts + "', 'YYYYMMDDHH24MISSFF3')";
    }
    public String changeFormatDate(String date){
    	String result = "";
    	SimpleDateFormat sdf = null;
    	if (!ConfigBundle.getValue("DEPLOY_TYPE").equals("P")) {
        	sdf = new SimpleDateFormat("yymmdd",new Locale("TH","th"));
    	} else {
        	sdf = new SimpleDateFormat("yymmdd");
    	}
    	if ((date != null && !date.equals("")) && sdf != null) {
    		try {
				Date d = sdf.parse(date);
				sdf.applyPattern("dd/mm/yyyy");
				result = sdf.format(d);
				String[] arrDate = result.split("\\/");
				result = arrDate[0] + "/"+arrDate[1]+"/"+arrDate[2];
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
    
    public String changeFormatDate2AD(String date){
    	String result = "";
    	SimpleDateFormat sdf = null;
    	if (!ConfigBundle.getValue("DEPLOY_TYPE").equals("P")) {
        	sdf = new SimpleDateFormat("yymmdd",new Locale("TH","th"));
    	} else {
        	sdf = new SimpleDateFormat("yymmdd");
    	}
    	if ((date != null && !date.equals("")) && sdf != null) {
    		try {
				Date d = sdf.parse(date);
				sdf.applyPattern("dd/mm/yyyy");
				result = sdf.format(d);
				String[] arrDate = result.split("\\/");
				result = arrDate[0] + "/"+arrDate[1]+"/"+(Integer.parseInt(arrDate[2])-543);
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
    
    public String getLastOfMonth(String yyyy, String mm){
    	int iYyyy = Integer.parseInt(yyyy);
    	int iMm = Integer.parseInt(mm);
    	Calendar cal = new GregorianCalendar(iYyyy, iMm, 0);
    	String result = "";
        Date date = cal.getTime();
        DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        result = sdf.format(date);
    	return result;
    }
    
    public String changeFormatDate2YYYYMMDDD(String date){
    	String result = "";
    	SimpleDateFormat sdf = null;
    	if (!ConfigBundle.getValue("DEPLOY_TYPE").equals("P")) {
        	sdf = new SimpleDateFormat("yymmdd",new Locale("TH","th"));
    	} else {
        	sdf = new SimpleDateFormat("yymmdd");
    	}
    	if ((date != null && !date.equals("")) && sdf != null) {
    		try {
				Date d = sdf.parse(date);
				sdf.applyPattern("dd/mm/yyyy");
				result = sdf.format(d);
				String[] arrDate = result.split("\\/");
				result = (Integer.parseInt(arrDate[2])-543) + ""+arrDate[1]+"" + arrDate[0];
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
    
    public String changeFormatDateFromCalendar(String date){
    	String result = "";
    	Calendar cal = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    	SimpleDateFormat sdf = new SimpleDateFormat("yymmdd");
    	if ((date != null && !date.equals("")) && sdf != null) {
    		try {
    			Date d = sdf.parse(date);
    			cal.setTime(d);
    			result = df.format(cal.getTime());
    		} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
    public boolean getWeekEnd(String yyyy, String mm, String dd){
    	boolean result = false;
		Date date= (new GregorianCalendar(Integer.parseInt(yyyy), Integer.parseInt(mm)-1, Integer.parseInt(dd))).getTime();
		SimpleDateFormat f = new SimpleDateFormat("EEEE" , LOCALE_EN);
		String day = f.format(date);
		if(day.equals("Sunday")){
			result = true;
		} else if (day.equals("Saturday")) {
			result = true;
		}
    	return result;
    }
}
